package com.atguigu.spzx.common.interceptor;

import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.tools.SpzxConsts;
import com.atguigu.spzx.util.AuthContextUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author LT
 * @create 2024-03-27-18:33
 */
@Component
public class SpzxAuthInterceptor implements HandlerInterceptor {

    @Resource
    RedisTemplate redisTemplate;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //请求访问的是需要登录的资源：登录状态在网关中已经校验了
        String token = request.getHeader("token");
        UserInfo userInfo = (UserInfo) redisTemplate.opsForValue().get(SpzxConsts.SPZX_USER_LOGIN_PREFIX + token);
        AuthContextUtil.set1(userInfo);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        AuthContextUtil.remove1();
    }
}
